package edu.chalmers.AV_2DPlatformerFramework.Model;

import edu.chalmers.AV_2DPlatformerFramework.Utility.Pair;
import edu.chalmers.AV_2DPlatformerFramework.Utility.Vector2D;
import java.util.ArrayList;

/**
 * Responsible for updating the GameThread for different models
 * @version 0.2
 * @author Richard
 * @revised Andrea, 29-04-2014: Refactoring
 */
public abstract class GameModel implements Runnable{
    
    private boolean running = false;
    private GameThread gameThread;
    private int currentTickRate;
    protected ArrayList<GameMap> maps;
    protected int currentMapIndex;
    
    /**
     * Returns the status of running.
     * @version 0.1
     * @author Andrea
     * @return
     */
    public boolean isRunning(){
    	return running;
    }
    
    /**
     * Set the status of running to opposite.
     * @version 0.1
     * @author Andrea
     * @param running The new Running state.
     */
    public void setRunning(boolean running){
    	this.running = running;
    }
    
    /**
     * Pauses the game by delaying the GameThread.
     * @version 0.2
     * @author Richard
     * @revised Andrea
     */
    public void pause(){
        if (isRunning()){
            setRunning(false);
            currentTickRate = gameThread.getDelay();
            gameThread.setDelay(1000000000);
            System.out.println("RESUME!");
        } 
        else {
            setRunning(true);
            gameThread.setDelay(currentTickRate);
            System.out.println("PAUSE!");
       }  
    }
    
    public ArrayList<GameObject> getActiveObjects(){
        return maps.get(currentMapIndex).getObjects();
    }
    
    //TODO kolla bounds m.m.
    public void setCurrentMapIndex(int index) {
        this.currentMapIndex = index;
    }
    public GameMap getCurrentGameMap(){
        return maps.get(currentMapIndex);
    }
    
    /**
     * This method is called by GameThread. Override to implement your own game.
     */
    @Override
    public void run(){
        System.out.println("Implement your own game by extending GameModel and overriding the run() method!");
    }
    
    /**'
     * Perform movement on all objects.
     * @author Richard Sundqvist
     * @version 1.0
     */
    public void moveActiveObjects(){
        ArrayList<GameObject> actors = maps.get(currentMapIndex).getObjects();
        Vector2D acc = maps.get(currentMapIndex).getGravity();
        
        Pair[] moves = new Pair[actors.size()];
        
        int i = 0;
        for(GameObject go : actors){
            moves[i] = new Pair(go, acc);
            i++;
        }
        ObjectMover.moveObjects(moves, actors);
    }
}